package com.bingxue.edu.framework.common.validation;

import cn.hutool.core.util.StrUtil;
import com.bingxue.edu.framework.common.constant.RegexConstants;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.regex.Pattern;

/**
 * @author panbo
 * @since 2023/8/20
 */
public class MobilePhoneValidation implements ConstraintValidator<MobilePhone, String> {

    private static final Pattern PATTERN = Pattern.compile(RegexConstants.MOBILE_PHONE);

    public static boolean match(String value) {
        if (StrUtil.isBlank(value)) {
            return false;
        } else {
            return PATTERN.matcher(value).matches();
        }
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        return match(value);
    }
}
